package zl.osimall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.osimall.common.bean.Goods;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.bean.Category;
import zl.osimall.common.mapper.CategoryMapper;
import zl.osimall.common.mapper.GoodsMapper;
import zl.osimall.common.service.CategoryService;

import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public boolean add(Category c) throws SteduException {
        if(c.getParentId() != 0 && categoryMapper.selectById(c.getParentId()) == null){
            throw new SteduException("子分类的父分类不得为空");
        }
        Category condition = new Category();
        condition.setName(c.getName());
        List<Category> categoryList = categoryMapper.selectByCondition(condition);
        if(!categoryList.isEmpty()){
            throw  new SteduException("该分类名已存在，无法重复添加");
        }
        return categoryMapper.insert(c) == 1;
    }

    @Override
    public boolean delete(Integer id) throws SteduException {
        Category category = categoryMapper.selectById(id);
        if(category == null){
            throw new SteduException("该分类不存在，无法删除");
        }
        if(category.getParentId() == 0){
            Category condition = new Category();
            condition.setParentId(id);
            if(categoryMapper.selectByCondition(condition).stream().count()>0){
                throw new SteduException("该分类下有子分类，请先删除子分类");
            }
        }else {
            Goods condition = new Goods();
            condition.setCategoryId(id);
            if(goodsMapper.selectByCondition(condition).stream().count()>0){
                throw new SteduException("该分类下有商品不能添加");
            }
        }
        return categoryMapper.delete(id)==1;
    }

    @Override
    public boolean update(Category c) throws SteduException {
        Category after = c;
        Category before = categoryMapper.selectById(c.getId());
        if(before == null){
            throw new SteduException("该分类不存在，无法修改");
        }
        if (after.getParentId()!=null && after.getParentId() == after.getId()){
            throw new SteduException("父类不得修改为继承自己的子类");
        }
        if(before.getParentId()==0 && after.getParentId()!=null &&after.getParentId() != 0){
            Category condition = new Category();
            condition.setParentId(c.getId());
            if(categoryMapper.selectByCondition(condition).stream().count()>0){
                throw new SteduException("当前分类存在子分类，无法修改为子分类");
            }
        }
        Category condition = new Category();
        if(Objects.equals(after.getParentId(), before.getParentId())){
            condition.setName(after.getName());
            condition.setParentId(after.getParentId());
            List<Category> categoryList = categoryMapper.selectByCondition(condition);
            if(categoryList.stream().anyMatch(item -> item.getId() != after.getId()) ){
                throw  new SteduException("该分类名已存在，无法重复添加");
            }
        }else {
            condition.setName(c.getName());
            condition.setParentId(after.getParentId());
            List<Category> categoryList = categoryMapper.selectByCondition(condition);
            if(categoryList!=null){
                throw  new SteduException("该分类名已存在，无法重复添加");
            }
        }

        return categoryMapper.update(c) == 1;
    }

    @Override
    public Category selectById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Category selectByName(String name) {
        return categoryMapper.selectByName(name);
    }

    @Override
    public List<Category> selectAllParent() {
        return categoryMapper.selectAllParent();
    }

    @Override
    public PageInfo<Category> selectByLike(Category c , Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Category> categories = categoryMapper.selectByLike(c);
        PageInfo<Category> pageInfo = new PageInfo<>(categories);
        return pageInfo;
    }

    @Override
    public List<Category> selectCondition(Category c) {
        return categoryMapper.selectByCondition(c);
    }
}
